//------------------------------------------------------------------------------
// Classification: COMPANY UNCLASSIFIED
//
// Copyright (c) Terma A/S
// All rights reserved
//------------------------------------------------------------------------------

package dk.snaveponyklub.persist.impl;

import java.io.*;
import java.net.URL;
import java.text.*;
import java.util.Date;
import java.util.List;

import javax.swing.text.DateFormatter;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

import dk.snaveponyklub.handler.Handler;
import dk.snaveponyklub.handler.impl.HandlerImpl;
import dk.snaveponyklub.persist.FilePersist;
import dk.snaveponyklub.persist.PersistFactory;
import dk.snaveponyklub.util.Logger;
import dk.snaveponyklub.xml.LoadXML;

public class FilePersistImpl implements FilePersist {

    private final LoadXML aLoadXML = new LoadXML();

    public FilePersistImpl() {
    }

    @Override
    public void addPerson(Document person) {
        SAXBuilder builder = new SAXBuilder();
        try {
            File file = new File("person.xml");
            Document document = builder.build(file);
            Namespace ns = Namespace.getNamespace("http://www.snaveponyklub.dk");
            Element root = document.getRootElement();
            Element personsElem = root.getChild("Persons", ns);
            // builder.setValidation(true);
            // builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            // "http://www.w3.org/2001/XMLSchema");
            // builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
            // "http://www.brics.dk/ixwt/examples/recipes.xsd");

            personsElem.addContent((Element) person.getRootElement().clone());
            Writer out = new StringWriter();
            XMLOutputter xo = new XMLOutputter();
            xo.output(document, out);
            FileWriter fw = new FileWriter(file);
            xo.output(document, fw);
            Logger.info(out.toString());
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Element getTimeSlot(Date starttime, Date endtime ) {
        try {
            Document document = new Document((Element)getTimeSlots(1).clone());
            XPath xp = XPath.newInstance("//s:timeslot[@starttime='2010-09-03T15:00:00' and @endtime='2010-09-03T16:00:00']");
            xp.addNamespace("s", "http://www.snaveponyklub.dk");
            Element timeslotNode = (Element)xp.selectSingleNode(document);
            //List timeslotNodes = xp.selectNodes(document);
            return timeslotNode;
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public Element getTimeSlots(int week) {
    	//Logger.info(DateFormat.getDateTimeInstance(0, 0).format(starttime));
    	//String starttime_str = new DateFormatter().
        SAXBuilder builder = new SAXBuilder();
        try {
        	//InputStream xf = ClassLoader.getSystemResourceAsStream("/timeslot.xml");
        	//InputStream xf = c.getResourceAsStream("/timeslot.xml");
            //File file = new File("/snaveponyklub/xsl/xml/timeslot.xml");
            File file = new File("/master/workspace/xml3/dist/snaveponyklub/xsl/xml/timeslot.xml");
            //File file = new File("timeslot.xml");
            Document document = builder.build(file);
            XPath xp = XPath.newInstance("//s:timeslots[.//s:timeslot[@starttime='2010-09-03T15:00:00' and @endtime='2010-09-03T16:00:00']]");
            //XPath xp = XPath.newInstance("//s:timeslots[.//s:timeslot[fn:contains(@starttime, '2010-09-03')]]");
            xp.addNamespace("s", "http://www.snaveponyklub.dk");
            xp.addNamespace("fn", "http://www.w3.org/2005/xpath-functions/");
            Element timeslotNode = (Element)xp.selectSingleNode(document);
            return timeslotNode;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
   
    public static void main(String[] args) throws IOException {
    	//FilePersist persist = PersistFactory.getInstance();
    	//Element timeslot = (new FilePersistImpl()).getTimeSlot(new Date(), new Date());
    	Element timeslot = (new FilePersistImpl()).getTimeSlots(1);
        XMLOutputter xo = new XMLOutputter();
        xo.output(timeslot, System.out);
    }
    
}